import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author xingzhiwen
 * @date 2021/9/14
 * @ddescription:
 */
@SuppressWarnings({"all"})

public class SocketTcp02Client {
    public static void main(String[] args) throws IOException {
        //1.连接服务端(ip 端口
            //如果；连接成功，返回socket对象
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("客户端+socket返回 = " + socket.getClass());
        //连接上后，生成socket，通过socket。getoutputstream 得到和socket对象关联的输出流
        OutputStream outputStream = socket.getOutputStream();
        //通过输出流，写入数据到数据通道
        outputStream.write("helloworld".getBytes());
        //写入结束标志
        socket.shutdownOutput();
        //获取socket相关联的输入流，读取数据，并显示
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int readLen = 0;
        while ((readLen = inputStream.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, readLen));
        }
        //设置结束标志

        //关闭流对象和socket，必须
        inputStream.close();

        outputStream.close();
        socket.close();

        System.out.println("客户端结束");
    }
}
